<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="600px">
      <el-form :model="formMaterial" label-position="right" :rules="rules" ref="formMaterial">
        <el-row>
          <el-col :span="24">
            <el-form-item label="物料代码" prop="code" :label-width="formLabelWidth">
              <el-input v-model="formMaterial.code" placeholder="请输入物料代码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="物料单位" prop="name" :label-width="formLabelWidth">
              <el-input v-model="formMaterial.name" placeholder="请输入物料单位"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    nccBasicUnitSave,
    nccBasicUnitUpdate
  } from '@/api/basic/PersonnelInformation.js'
  export default {
    props: {
      visible: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        formMaterial: {
          code: '',
          name: '',
        },
        title: '新增物料',
        formLabelWidth: '110px',
        rules: {
          code: [{
            required: true,
            message: '请输入物料代码',
            trigger: 'blur'
          }],
          name: [{
            required: true,
            message: '请输入物料名称',
            trigger: 'blur'
          }]
        },
      };
    },
    methods: {
      //获取当前行信息
      openDialog(type, val) {
        this.title = type === 'edit' ? '编辑物料' : '新增物料';
        // 根据类型区分新增和编辑逻辑
        if (type === 'edit' && val) {
          this.formMaterial = {...val};
        } else {
          this.formMaterial = {
            code: '',
            name: '',
          }
        }
      },
      // 提取重置表单的逻辑
      handleClose() {
        // 通过事件通知父组件更新visible值，触发update:visible事件
        this.$emit('update:visible', false);
        this.$nextTick(() => {
          this.$refs.formMaterial.resetFields();
        })
      },
      //提交表单
      submit() {
        this.$refs.formMaterial.validate((valid) => {
          if (valid) {
            const apiMethod = this.title === '新增物料' ? nccBasicUnitSave : nccBasicUnitUpdate;
            // 调用相应的 API 方法
            apiMethod(this.formMaterial).then(res => {
              this.$emit('addSuccess'); // 通知父组件添加成功
              this.$message({
                message: res.msg, // 显示成功消息
                type: 'success',
              });
              // 提交成功后可以选择清空表单
              this.handleClose();
            }).catch(err => {
              this.$message({
                message: err?.msg || '操作失败', // 捕获错误信息
                type: 'error',
              });
            });
          } else {
            // 校验失败
            return false;
          }
        });
      }
    },
  };
</script>

<style></style>
